package com.my.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.entity.Admin;
import com.my.entity.AskLeave;
import com.my.service.AskLeaveService;
import com.my.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("leave")
public class AskLeaveController {

    @Autowired
    private AskLeaveService service;

    @GetMapping("list")
    public String list(Model model, @RequestParam(defaultValue = "1") int pageNo, AskLeave leave) {
        model.addAttribute("page", service.getList(new Page<>(pageNo, 10), leave));
        model.addAttribute("sName", leave.getSName());
        model.addAttribute("type", leave.getType());
        model.addAttribute("status", leave.getStatus());
        return "leave/list";
    }

    @PostMapping("review")
    @ResponseBody
    public Rest review(Long id, Integer status, HttpSession session) {
        Admin admin = ((Admin) session.getAttribute("CURRENT_ADMIN"));
        AskLeave leave = new AskLeave().setId(id).setStatus(status).setApprovalId(admin == null ? 0 : admin.getId()).setApprovalType(1);
        if(service.updateById(leave)) {
            return Rest.ok("审核成功！");
        }
        return Rest.err("审核错误！");
    }

}
